package com.audaque.springboot.foshanupload.redis.config;


import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

/**
 * redisson配置
 */
@Slf4j
@Configuration
@ConditionalOnProperty(value = "switch.redis",havingValue = "true",matchIfMissing = false)
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;


    @Value("${spring.redis.database:0}")
    private int database;

    @Value("${spring.redis.password}")
    private String password;

     //方式1
    @Bean
    public RedissonClient getRedisson() throws Exception{
        RedissonClient redisson = null;
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + host + ":" + port).setDatabase(database).setPassword(password).setDnsMonitoringInterval(-1);
        // log.debug("redisson配置,addressUrl："+addressUrl);
        redisson = Redisson.create(config);
        return redisson;
    }
    //方式2
/*
    @Bean
    public RedissonClient redissonClient() throws IOException {
    //fromJSON
        Config config = Config.fromYAML(new ClassPathResource("redisson-cluster.yml").getInputStream());
        return Redisson.create(config);
    }
    */


}
